<template>
  <div class="box">
    <div class="left">
      <div class="tab">
        <div
          v-for="(v, i) in tabLabel"
          :key="i"
          :class="tabIndex == i ? 'tabActive tabItem' : 'tabItem'"
          @click="tabFn(i)"
        >
          {{ v }}
        </div>
      </div>
      <div>
        <el-input
          placeholder="请输入分群编号"
          suffix-icon="el-icon-search"
          v-model="selectValue"
          @change="search()"
        >
        </el-input>
      </div>
      <div class="searchList">
        <div
          v-for="v in searchList"
          :key="v.id"
          class="searchListItem"
          @click="detail(v.id)"
        >
          {{ v.name }}
        </div>
      </div>
    </div>
    <div class="right">
      <!-- 分群信息 -->
      <div class="card">
        <div class="title">分群信息</div>
        <div class="info">
          <div class="infoItem">
            <div>
              <span class="infoItemLabel">分群编号</span
              ><span>{{ groupDefine.groupId }}</span>
            </div>
            <div>
              <span class="infoItemLabel">分群名称</span
              ><span>{{ groupDefine.groupNm }}</span>
            </div>
          </div>
          <div class="infoItem">
            <div>
              <span class="infoItemLabel">分群规则</span
              ><span>{{ groupDefine.groupRule }}</span>
            </div>
            <div>
              <span class="infoItemLabel">分群标签</span
              ><span>{{ groupDefine.groupCount }}</span>
            </div>
          </div>
        </div>
      </div>
      <!-- 分群成员明细 -->
      <div class="card group-detail">
        <div class="title">分群成员明细</div>
        <div class="btn-group">
          <!-- <el-button type="primary" class="btn" @click="handleAdd"
            >增加</el-button
          >
          <el-button type="primary" class="btn" @click="handleDelete"
            >删除</el-button
          > -->
        </div>
        <template v-if="tabIndex == 0">
          <el-table
            :data="tableData"
            style="width: 100%"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="55"> </el-table-column>
            <el-table-column
              prop="customerID"
              label="客户编号"
              min-width="140"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="customerNM"
              label="客户名称"
              min-width="140"
              align="center"
            >
            </el-table-column>
            <!-- <el-table-column
            prop="custRating"
            label="信用评级"
            min-width="90"
            align="center">
          </el-table-column> -->
            <el-table-column
              prop="cusType"
              label="客户类型"
              min-width="90"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="cumlativeSize"
              label="累计债券只数"
              min-width="90"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="cumlativeAmount"
              label="累计销售规模(万元)"
              min-width="90"
              align="center"
              :formatter="formatterNum"
            >
            </el-table-column>
          </el-table>
        </template>
        <template v-else>
          <el-table
            :data="tableData"
            style="width: 100%"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="55"> </el-table-column>
            <el-table-column
              prop="bondId"
              label="债券编号"
              min-width="140"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="bondFname"
              label="债券名称"
              min-width="140"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="bondSname"
              label="债券简称"
              min-width="90"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="issueAmount"
              label="发行规模（万元）"
              :formatter="formatterNum"
              min-width="90"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="bondType"
              label="债券类型"
              min-width="90"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="termDays"
              label="期限（天）"
              min-width="90"
              align="center"
            >
            </el-table-column>
          </el-table>
        </template>
        <el-pagination
          small
          background
          layout="prev, pager, next"
          :page-count="pageInfo.pageCount"
          :page-size="pageInfo.pageSize"
          :current-page="pageInfo.pageNum"
          :total="pageInfo.total"
          @current-change="groupMemberChange"
        >
        </el-pagination>
      </div>
    </div>
    <el-dialog
      title="请选择"
      :before-close="handleClose"
      :visible.sync="dialogVisible"
      width="50%"
    >
      <template v-if="tabIndex == 0">
        <el-row>
          <el-col :span="24">
            <div class="form-item">
              <label class="label">客户编号</label>
              <el-select
                v-model="customerId"
                placeholder="请选择"
                clearable
                filterable
                remote
                reserve-keyword
                :remote-method="queryCustomerIdcommon"
                :loading="loading"
              >
                <el-option
                  v-for="item in customerNameOptions"
                  :key="item.value"
                  :label="item.value"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <div class="form-item">
              <label class="label">客户名称</label>
              <el-select
                v-model="customerId"
                placeholder="请选择"
                clearable
                filterable
                remote
                reserve-keyword
                :remote-method="queryCustomerNamecommon"
                :loading="loading"
              >
                <el-option
                  v-for="item in customerNameOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </div>
          </el-col>
        </el-row>
      </template>
      <template v-else>
        <el-row>
          <el-col :span="24">
            <div class="form-item">
              <label class="label">债券编码</label>
              <el-select
                v-model="bondId"
                placeholder="请选择"
                clearable
                filterable
                remote
                reserve-keyword
                :remote-method="queryBondIdcommon"
                :loading="loading"
              >
                <el-option
                  v-for="item in bondNameOptions"
                  :key="item.value"
                  :label="item.value"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <div class="form-item">
              <label class="label">债券名称</label>
              <el-select
                v-model="bondId"
                placeholder="请选择"
                clearable
                filterable
                remote
                reserve-keyword
                :remote-method="queryBondNamecommon"
                :loading="loading"
              >
                <el-option
                  v-for="item in bondNameOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </div>
          </el-col>
        </el-row>
      </template>
      <span slot="footer" class="dialog-footer">
        <el-button class="btn" @click="dialogVisible = false">取 消</el-button>
        <el-button class="btn" type="primary" @click="handAddSubmit"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getCustomerGroupcodes,
  getBondGroupcodes,
  getCustomerGroupdefine,
  getBondGroupdefine,
  getCustomerGroupmember,
  getBondGroupmember,
  addCustomerGroupmember,
  addBondGroupmember,
  deleteCustomerGroupmember,
  deleteBondGroupmember,
  getBondcommon,
  getCustomercommon,
} from "@/api/eigen/groupManagement.js";

var FormData = require("form-data");

export default {
  data() {
    return {
      tabIndex: 0,
      detailId: "",
      tabLabel: ["客户分群列表", "债券分群列表"],
      tableData: [],
      searchInput: "",
      riskAppetiteDate: "",
      selectValue: "",
      searchList: [],
      groupDefine: {},
      multipleSelection: [],
      pageInfo: {
        pageCount: 0,
        pageNum: 1,
        pageSize: 10,
        total: 0,
      },
      dialogVisible: false,
      loading: false,
      customerId: "",
      customerNameOptions: [
        {
          label: "",
          code: "",
        },
      ],
      bondId: "",
      bondNameOptions: [
        {
          label: "",
          code: "",
        },
      ],
    };
  },
  mounted() {
    let _this = this;
    this.$nextTick(() => {
      this.getCustomerGroupCodes();
    });
  },
  methods: {
    formatterNum(row, cloumn, cellValue, index) {
      const str = cellValue;
      if (str) {
        const reg =
          str.indexOf(".") > -1 ? /\B(?=(\d{3})+\.)/g : /\B(?=(\d{3})+$)/g;
        const returnData = str.replace(reg, ",");
        return returnData;
      } else {
        return "";
      }
    },
    tabFn(i) {
      this.tabIndex = i;
      this.tableData = [];
      this.pageInfo = {
        pageCount: 0,
        pageNum: 1,
        pageSize: 10,
        total: 0,
      };
      if (i == 0) {
        this.getCustomerGroupCodes();
      } else {
        this.getBondGroupCodes();
      }
    },
    search() {
      if (this.tabIndex == 0) {
        this.getCustomerGroupCodes();
      } else {
        this.getBondGroupCodes();
      }
    },
    detail(id) {
      this.detailId = id;
      if (this.tabIndex == 0) {
        this.getCustomerGroupmember(id);
        this.getCustomerGroupdefine(id);
      } else {
        this.getBondGroupmember(id);
        this.getBondGroupdefine(id);
      }
    },
    handleAdd() {
      this.dialogVisible = true;
    },
    handleClose(done) {
      this.dialogVisible = false;
    },
    handAddSubmit() {
      let _this = this;
      _this.dialogVisible = false;
      if (this.tabIndex == 0) {
        if (_this.detailId && _this.customerId) {
          addCustomerGroupmember({
            groupId: this.detailId.trim(),
            objectId: _this.customerId.trim(),
          }).then((res) => {
            _this.pageInfo.pageNum = 1;
            _this.detail(_this.detailId.trim());
          });
        }
      } else {
        if (_this.detailId && _this.bondId) {
          addBondGroupmember({
            groupId: this.detailId.trim(),
            objectId: _this.bondId.trim(),
          }).then((res) => {
            _this.pageInfo.pageNum = 1;
            _this.detail(_this.detailId.trim());
          });
        }
      }
    },
    handleDelete() {
      let _this = this;
      let params = {
        groupId: _this.detailId.trim(),
        objectIds: [],
      };
      console.info(_this.multipleSelection);
      if (_this.tabIndex == 0) {
        _this.multipleSelection.forEach((item) => {
          params.objectIds.push(item.customerID);
        });
        _this.deleteCustomerGroupmember(params);
      } else {
        _this.multipleSelection.forEach((item) => {
          params.objectIds.push(item.bondId);
        });
        _this.deleteBondGroupmember(params);
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    groupMemberChange(pageNum) {
      this.pageInfo.pageNum = pageNum;
      if (this.tabIndex == 0) {
        this.getCustomerGroupmember(this.detailId);
      } else {
        this.getBondGroupmember(this.detailId);
      }
    },
    getCustomerGroupCodes() {
      let _this = this;
      getCustomerGroupcodes({
        groupIdOrName: _this.selectValue,
      }).then((res) => {
        _this.searchList = [];
        res.data.forEach((item) => {
          _this.searchList.push({
            id: item.groupId,
            name: item.groupNm,
          });
        });
      });
    },
    getBondGroupCodes() {
      let _this = this;
      getBondGroupcodes({
        groupIdOrName: _this.selectValue,
      }).then((res) => {
        _this.searchList = [];
        res.data.forEach((item) => {
          _this.searchList.push({
            id: item.groupId,
            name: item.groupNm,
          });
        });
      });
    },
    getCustomerGroupdefine(id) {
      let _this = this;
      getCustomerGroupdefine({
        groupId: id,
      }).then((res) => {
        if (res.data != null) {
          _this.groupDefine = res.data;
        }
      });
    },
    getBondGroupdefine(id) {
      let _this = this;
      getBondGroupdefine({
        groupId: id,
      }).then((res) => {
        if (res.data != null) {
          _this.groupDefine = res.data;
        }
      });
    },
    getCustomerGroupmember(id) {
      let _this = this;
      getCustomerGroupmember({
        groupId: id,
        pn: _this.pageInfo.pageSize,
        page: _this.pageInfo.pageNum,
      }).then((res) => {
        _this.tableData = [];
        _this.tableData = res.data.result;
        _this.pageInfo.total = parseInt(res.data.totalResults);
      });
    },
    getBondGroupmember(id) {
      let _this = this;
      getBondGroupmember({
        groupId: id,
        pn: _this.pageInfo.pageSize,
        page: _this.pageInfo.pageNum,
      }).then((res) => {
        _this.tableData = [];
        _this.tableData = res.data.result;
        _this.pageInfo.total = parseInt(res.data.totalResults);
      });
    },
    queryCustomerNamecommon(query) {
      let _this = this;
      if (query) {
        _this.loading = true;
        getCustomercommon({
          customerNM: query,
        }).then((res) => {
          _this.loading = false;
          _this.customerNameOptions = [];
          res.data.result.forEach((item) => {
            _this.customerNameOptions.push({
              label: item.customerNM,
              value: item.customerID,
            });
          });
        });
      } else {
        _this.customerNameOptions = [];
      }
    },
    queryCustomerIdcommon(query) {
      let _this = this;
      if (query) {
        _this.loading = true;
        getCustomercommon({
          customerId: query,
        }).then((res) => {
          _this.loading = false;
          _this.customerNameOptions = [];
          res.data.result.forEach((item) => {
            _this.customerNameOptions.push({
              label: item.customerNM,
              value: item.customerID,
            });
          });
        });
      } else {
        _this.customerNameOptions = [];
      }
    },
    queryBondNamecommon(query) {
      let _this = this;
      if (query) {
        _this.loading = true;
        getBondcommon({
          bondName: query,
        }).then((res) => {
          _this.loading = false;
          _this.bondNameOptions = [];
          res.data.result.forEach((item) => {
            _this.bondNameOptions.push({
              label: item.bondFname,
              value: item.bondId,
            });
          });
        });
      } else {
        _this.bondNameOptions = [];
      }
    },
    queryBondIdcommon(query) {
      let _this = this;
      if (query) {
        _this.loading = true;
        getBondcommon({
          bondId: query,
        }).then((res) => {
          _this.loading = false;
          _this.bondNameOptions = [];
          res.data.result.forEach((item) => {
            _this.bondNameOptions.push({
              label: item.bondFname,
              value: item.bondId,
            });
          });
        });
      } else {
        _this.bondNameOptions = [];
      }
    },
    deleteCustomerGroupmember(params) {
      let _this = this;
      deleteCustomerGroupmember(params).then((res) => {
        _this.pageInfo.pageNum = 1;
        _this.detail(_this.detailId.trim());
      });
    },
    deleteBondGroupmember(params) {
      let _this = this;
      deleteBondGroupmember(params).then((res) => {
        _this.pageInfo.pageNum = 1;
        _this.detail(_this.detailId.trim());
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.el-pagination {
  margin-top: 10px;
  text-align: right;
}

.el-select {
  width: 50%;
}

.box {
  display: flex;

  .riskAppetiteTable {
    padding-top: 10px;
    padding-right: 10px;

    .el-input__inner {
      background-color: #363657 !important;
      color: #fff;
      border: 1px solid rgba(126, 163, 255, 0.3);
    }
  }

  .riskAppetiteTable ::v-deep .el-range-input {
    background-color: #363657 !important;
    color: #fff;
  }

  .riskAppetiteSelect {
    margin: 0 5px;
  }

  .riskAppetiteSelect ::v-deep .el-input__inner {
    height: 31px !important;
    line-height: 31px !important;
  }

  .el-select-dropdown {
    padding: 10px 0px !important;
  }

  .left {
    width: 300px;
    background-color: #262d47;
    height: 100%;
    padding: 10px 10px;
  }

  .right {
    margin-left: 10px;
    flex: 1;
    display: flex;
    flex-direction: column;
  }

  .tab {
    display: flex;
    height: 50px;
    line-height: 50px;
    text-align: center;
    margin-bottom: 10px;
    color: #959fc5;

    div {
      flex: 1;
      cursor: pointer;
    }

    .tabActive {
      background-color: #1c2238 !important;
      color: #fff;
    }
  }

  .searchList {
    padding-top: 10px;
    max-height: 800px;
    height: 100%;
    overflow-y: scroll;

    .searchListItem {
      padding-left: 10px;
      padding-right: 10px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      background-color: #363657;
      color: #959fc5;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }

    .searchListItem:hover {
      background-color: #1c2238 !important;
      color: #fff;
      cursor: pointer;
    }
  }

  .form-item {
    padding: 5px 10px;

    .label {
      font-size: 12px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #ffffff;
      margin-right: 20px;
    }
  }

  .card {
    padding: 10px 10px;
    background-color: #262d47;
    color: #959fc5;

    .title {
      font-size: 16px;
    }

    .info {
      display: flex;
      padding: 10px 10px;

      .infoItem {
        flex: 1;

        div:not(:last-child) {
          margin-bottom: 10px;
        }
      }
    }
  }

  .group-detail {
    height: 100%;
    margin-top: 10px;
  }

  .btn-group {
    padding: 10px 5px;
  }

  .infoItemLabel {
    margin-right: 10px;
  }

  .el-table ::v-deep th.el-table__cell {
    background-color: #1c2238 !important;
    color: #fff;
  }
}
</style>
